#include "sessiondetailwidget.h"

#include <QVBoxLayout>
#include <QFont>
#include <QGridLayout>
#include "debug.h"

AvatarItem::AvatarItem(const QIcon& avatar, const QString& nickname)
{
    // 1. 设置固定大小
    this->setFixedSize(QSize(53, 60));

    // 2. 设置纵置布局
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->setSpacing(9);
    vlayout->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); // 竖直方向居中??
    this->setLayout(vlayout);

    // 3. 创建头像
    user_avatar = new QPushButton();
    user_avatar->setFixedSize(QSize(40, 40));
    user_avatar->setIconSize(QSize(40, 40));
    user_avatar->setIcon(avatar);
    user_avatar->setStyleSheet("QPushButton { border: none; }");
    vlayout->addWidget(user_avatar);

    // 4. 创建昵称
    user_nickname = new QLabel();
    user_nickname->setText(nickname);
    QFont font("微软雅黑", 8);
    user_nickname->setFont(font);
    user_nickname->setAlignment(Qt::AlignCenter);
    user_nickname->setStyleSheet("QLabel { color: rgb(68, 68, 68); }");
    vlayout->addWidget(user_nickname);

    // user_nickname->setStyleSheet("QLabel { color: rgb(68, 68, 68); }");

    // 5. 截断
    int max_length = 50;
    QFontMetrics metrics(font);
    int name_length = metrics.horizontalAdvance(nickname);
    if (name_length > max_length)
    {
        QString tail = "...";
        int tail_length = metrics.horizontalAdvance(tail);
        int available_length = max_length - tail_length;

        QString new_nickname;
        int current_width = 0;

        for (int i = 0; i < nickname.size(); ++i)
        {
            int char_width = metrics.horizontalAdvance(nickname[i]);
            if (current_width + char_width > available_length)
                break;

            new_nickname += nickname[i];
            current_width += char_width;
        }

        new_nickname += tail;
        user_nickname->setText(new_nickname);
    }


    // if (name_length >= max_length)
    // {
    //     QString tail = "...";
    //     int tail_length = metrics.horizontalAdvance(tail);
    //      // qDebug() << "tail_length: " << tail_length;

    //     int available_length = max_length - tail_length;
    //     // 估算 nickname 可以保留多少字符
    //     int available_size = nickname.size() * (1.0 * available_length / name_length);
    //     QString new_nickname = nickname.left(available_size) + tail;
    //     user_nickname->setText(new_nickname);
    // }
}


SessionDetailWidget::SessionDetailWidget(QWidget* parent, const UserInfo& userinfo)
{
    // 1. 设置 Dialog 基本属性
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setWindowTitle("会话详情");
    this->setFixedSize(QSize(200, 220));
    this->setStyleSheet("QWidget { background-color: rgb(245, 245, 245); }");
    this->setAttribute(Qt::WA_DeleteOnClose); // 关闭时自动删除对象

    // 2. 创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(10, 10, 10, 10);
    layout->setSpacing(0);
    this->setLayout(layout);

    // 4. 添加“添加”按钮
    AvatarItem* add_btn = new AvatarItem(QIcon(":resource/image/add.png"), "添加");
    layout->addWidget(add_btn, 0, 0, 1, 1, Qt::AlignHCenter | Qt::AlignVCenter);

    // 3. 添加测试数据
#ifdef UI_TEST
    AvatarItem* test_user = new AvatarItem(QIcon(":resource/image/myAvatar.jpg"), "月朗天门1212");
    layout->addWidget(test_user, 0, 1, 1, 1, Qt::AlignHCenter | Qt::AlignVCenter);
#endif

    // 5. 添加“删除好友”按钮
    delete_friend_btn = new QPushButton();
    delete_friend_btn->setText("删除");

    QString style = "QPushButton { background-color: transparent; border-radius: 5px; }";
    style += "QPushButton::pressed { background-color: rgb(209, 209, 209); }";
    style += "QPushButton { color: rgb(244, 84, 84); font-size: 16px; }";
    delete_friend_btn->setStyleSheet(style);
    delete_friend_btn->setFixedHeight(67);
    layout->addWidget(delete_friend_btn, 1, 0, 1, 2, Qt::AlignVCenter);
}


void SessionDetailWidget::DeleteFriend()
{}
